package com.sxit.ecwx.service.imp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

import com.sxit.ecwx.core.utils.CloseSth;
import com.sxit.ecwx.core.utils.EcwxUtils;
import com.sxit.ecwx.core.utils.Formatu;
import com.sxit.ecwx.core.utils.Log;
import com.sxit.ecwx.model.bean.BasePushMsg;

/**
 * 
 * 面向IM平台的接口管理.
 * 
 */
public class ConnectServer 
{

    private static final ConcurrentLinkedQueue<BasePushMsg> msgs = new ConcurrentLinkedQueue<BasePushMsg>();
    private Socket sock = null;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    private static int currentStatisCount=0;
    public  static String coreip="124.238.219.81";
    public static int coreport=9001;

    /** 事务ID发生器. */
    public static final AtomicInteger ai = new AtomicInteger((int) (Formatu.rand() & 0xFFFFFFFF));



    /** 发送字节流到core模块. */
    public int send2Core(byte by[])
    { 	int result=0;
      
        try
        {
        	System.out.println("开始发送数据，。。。。。。。。。。。");
            this.dos.write(by);
            this.dos.flush();
            System.out.println("发送数据完毕，，，，，，，等待返回值！！！！");
           int len = this.dis.readInt();
           System.out.println("返回值长度=="+len);
           
           byte[] nPacketLen=new byte[48];
           byte[] nOperID=new byte[4];
           byte[] seq=new byte[36];
           byte[] nReturn=new byte[4];
           
           System.arraycopy(Formatu.int2byte(len), 0, nPacketLen, 0, 4);
           //System.out.println(Arrays.toString(nPacketLen));
           this.dis.readFully(nPacketLen, 4, len - 4);
          // System.out.println(Arrays.toString(nPacketLen));
           int operid=  Formatu.byte2int(nPacketLen,4);
           System.out.println("命令字："+operid);
           System.arraycopy(nPacketLen, 8, seq, 0, 36);
           //System.out.println(Arrays.toString(seq));
           String  cmd=new String(seq);
           System.out.println("序列："+cmd);
           System.arraycopy(nPacketLen, 44, nReturn, 0, 4);         
           result=Formatu.byte2int(nReturn,0);
           System.out.println("返回值为："+result);
          
           CloseSth.closeInputStream(this.dis);
           CloseSth.closeOutputStream(this.dos);
           CloseSth.closeSocket(this.sock);
           System.out.println("关闭流结束！！！！！！！！");
          
           
        } catch (Exception e)
        {
            System.out.println("socket 写入出错 ：" + e);
            CloseSth.closeInputStream(this.dis);
            CloseSth.closeOutputStream(this.dos);
            CloseSth.closeSocket(this.sock);
            System.out.println("关闭流结束！！！！！！！！");
            Log.error(Log.trace(e));
            return result;
        }
        return result;
    }


    /** 尝试建立到IM系统core模块的连接. 
     * @throws IOException */
    public void connect2Core(String ip, int port) throws IOException
    {
        
    	    System.out.println("ip=="+ip+",port=="+port);
            this.sock = new Socket(ip, port);
            this.dis = new DataInputStream(this.sock.getInputStream());
            this.dos = new DataOutputStream(this.sock.getOutputStream());
            Log.info("got a connection with core, %s:%d\n", ip, port);
              
    }
}
